﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Collections;
using System.IO;
using System.Text;

using Web.SystemConfig.ArchiveCode;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.Services;
using DreamBuilder.Core.Container;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Enum;
using CommonModule;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;

/*
 * 打印说明
 * 卷格式存储的档案，一卷打印一页
 * 件格式存储的档案，选择多个后打印一页
 * 卷：会计、基建、科研、设备、油气勘探、
 * 
 * 作者: jiejiep
 * 邮箱: ling121211@gmail.com
 * 时间: 2014年6月22日 13:00:11
 */

namespace Web.Views.ArchiveFlowView
{
    public partial class PrintBeikaoTableView : PageBase
    {

        private const string _pageBreakHTML = @"<hr align='center' width='100%' size='1' noshade style='display:none;' />
    <div style='page-break-after:always;'></div>";

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        private int _baseArchiveTypeId;

        public int BaseArchiveTypeId
        {
            get
            {
                if (_baseArchiveTypeId == 0)
                {
                    _baseArchiveTypeId = ArchiveMenuService.GetArchiveBaseIDById(int.Parse(Request.QueryString["archiveType"]));
                }
                return _baseArchiveTypeId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            WriteUserLog(string.Format("用户{0}打印了{1}类型档案的备考表",
                CurrentUser.Real_Name, BaseArchiveTypeId), OracleLogType.ArchiveManage);

            ArrayList idList = null;
            if (Request.QueryString["from"].Equals("dazl"))     //信息中心-E6 档案整理
            {
                idList = Session["dazl_bkb_selectedkeys"] as ArrayList;
            }
            else if (Request.QueryString["from"].Equals("sjgl")) //数据管理
            {
                idList = Session["sjgl_bkb_selectedkeys"] as ArrayList;
            }
            else if (Request.QueryString["from"].Equals("dasj")) //档案上交
            {
                idList = Session["dasj_bkb_selectedkeys"] as ArrayList;
            }
            List<string> myIdList = new List<string>();
            foreach (var item in idList)
            {
                myIdList.Add(item.ToString());
            }

            StringBuilder sb = new StringBuilder();

            DataTable table = null;
            // 页数
            int pageCount = 0;
            // 件数
            int fileCount = 0;
            // 是否为卷
            bool isJuan = true;

            switch (BaseArchiveTypeId)
            {
                //文书类
                case ((int)ArchiveBaseType.Document):
                    isJuan = false;
                    IDocumentArchiveService documentArchiveService = UContainer.Resolve<IDocumentArchiveService>();
                    table = documentArchiveService.GetDocumentArchiveDataByIds(myIdList);
                    pageCount = myIdList.Count;
                    break;
                //会计类-卷
                case ((int)ArchiveBaseType.Account):
                    IAccountArchiveService accountArchiveService = UContainer.Resolve<IAccountArchiveService>();
                    table = accountArchiveService.GetAccountArchiveDataByIds(myIdList);
                    break;
                //基建类-卷
                case ((int)ArchiveBaseType.BulidProject):
                    IBuildProjectArchiveService buildProjectService = UContainer.Resolve<IBuildProjectArchiveService>();
                    table = buildProjectService.GetBuildProjectArchiveDataByIds(myIdList);
                    break;
                //设备类-卷
                case ((int)ArchiveBaseType.Equipment):
                    IEquipmentService equipmentService = UContainer.Resolve<IEquipmentService>();
                    table = equipmentService.GetEquipmentArchiveDataByIds(myIdList);
                    break;
                //油气勘探类
                case ((int)ArchiveBaseType.OliSearch):
                    isJuan = false;
                    IOilSearchArchiveService oilSearchService = UContainer.Resolve<IOilSearchArchiveService>();
                    table = oilSearchService.GetOilSearchArchiveDataByIds(myIdList);
                    break;
                //音像类
                case ((int)ArchiveBaseType.AudioVedio):
                    isJuan = false;
                    IAudioArchiveService audioService = UContainer.Resolve<IAudioArchiveService>();
                    table = audioService.GetAudioArchiveDataByIds(myIdList);
                    pageCount = myIdList.Count;
                    break;
                //实物类
                case ((int)ArchiveBaseType.RealKind):
                    isJuan = false;
                    IRealKindArchiveService realService = UContainer.Resolve<IRealKindArchiveService>();
                    table = realService.GetRealKindArchiveDataByIds(myIdList);
                    pageCount = myIdList.Count;
                    break;
                //科研类-卷
                case ((int)ArchiveBaseType.Technology):
                    ITechArchiveService techService = UContainer.Resolve<ITechArchiveService>();
                    table = techService.GetTechArchiveDataByIds(myIdList);
                    break;
                //光盘类
                case ((int)ArchiveBaseType.CDKind):
                    isJuan = false;
                    ICDArchiveService cdService = UContainer.Resolve<ICDArchiveService>();
                    table = cdService.GetCDArchiveDataByIds(myIdList);
                    pageCount = myIdList.Count;
                    break;
            }

            //页号，从0开始
            int pageIndex = 0;
            
            //判断是否为卷
            if (isJuan)
            {
                //单页模版
                string templateJuanPage = File.ReadAllText(Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    @"Views\ArchiveFlowView\PrintBeikaoJuanTable.htm"));

                sb.Append("<br />");
                foreach (DataRow row in table.Rows)
                {
                    
                    //没有卷内件：页数-取卷的页数，件加1
                    pageCount = row["pagenum"] == DBNull.Value ? 0 : int.Parse(row["pagenum"].ToString());
                    fileCount = row["pagefilenum"] == DBNull.Value ? 0 : int.Parse(row["pagefilenum"].ToString());

                    sb.Append(templateJuanPage.Replace("{jian}", fileCount.ToString())
                            .Replace("{ye}", pageCount.ToString()));

                    pageIndex++;
                    if (pageIndex >= 1 && pageIndex < table.Rows.Count)
                    {
                        
                        sb.Append(_pageBreakHTML);
                        sb.Append("<br />");
                    }
                    

                }
            }
            else
            {
                //单页模版
                string templatePage = File.ReadAllText(Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, @"Views\ArchiveFlowView\PrintBeikaoTable.htm"));
                int zheng = int.Parse(table.Compute("sum(mainbodynum)", "").ToString());
                int fu = int.Parse(table.Compute("sum(doplicatebodynum)", "").ToString());
                int sum = zheng + fu;
                fileCount = table.Rows.Count;
                sb.Append(templatePage.Replace("{jian}", fileCount.ToString())
                            .Replace("{zheng}", zheng.ToString())
                            .Replace("{fu}", fu.ToString())
                            .Replace("{sum}", sum.ToString()));

            }


            //打印页面
            string printText = File.ReadAllText(Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, @"Views\ArchiveFlowView\PrintBeikaoTableFrame.htm"));

            Session["bkbPrint"] = printText.Replace("{content}", sb.ToString());
            
            Response.Redirect("PageForPrint.aspx?from=bkb");
        }
    }
}